package xin.marcher.module.quality.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Repository;
import xin.marcher.module.common.enums.DelFlagEnum;
import xin.marcher.module.common.enums.YesOrNoEnum;
import xin.marcher.module.common.enums.construction.ProductExceptionCode;
import xin.marcher.module.common.enums.inventory.InventoryExceptionCode;
import xin.marcher.module.common.enums.quality.ClaimedStatusEnum;
import xin.marcher.module.common.enums.quality.QualityAuditStatusEnum;
import xin.marcher.module.common.enums.quality.QualitySurveyStatusEnum;
import xin.marcher.module.common.enums.quality.QualityUploadStatusEnum;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.common.exception.CommonErrorCodeEnum;
import xin.marcher.module.common.exception.ProductBizException;
import xin.marcher.module.common.utils.LocalDateTimeUtil;
import xin.marcher.module.quality.converter.ItemQualityControlConverter;
import xin.marcher.module.quality.domain.dto.ItemQualityControlDataDTO;
import xin.marcher.module.quality.domain.dto.ItemQualityControlResultDTO;
import xin.marcher.module.quality.domain.dto.ItemQualityControlSurveyDTO;
import xin.marcher.module.quality.domain.dto.ItemQualityControlSurveyTaskDTO;
import xin.marcher.module.quality.domain.entity.*;
import xin.marcher.module.quality.domain.request.ItemQualityControlDataRequest;
import xin.marcher.module.quality.domain.request.ItemQualityControlSurveyRequest;
import xin.marcher.module.quality.domain.request.ItemQualityControlSurveyTaskRequest;
import xin.marcher.module.quality.mapper.*;
import xin.marcher.module.rights.support.LoginUser;
import xin.marcher.module.rights.support.LoginUserHolder;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static xin.marcher.module.common.constants.construction.ProductConstants.QUERY_ITEM_MAX_COUNT;

/**
 * 品控资源管理
 */
@Repository
public class ItemQualityControlRepository {

    @Resource
    private ItemQualityControlDataMapper itemQualityControlDataMapper;

    @Resource
    private ItemQualityControlResultMapper itemQualityControlResultMapper;

    @Resource
    private ItemQualityControlSurveyMapper itemQualityControlSurveyMapper;

    @Resource
    private ItemQualityControlSurveyTaskMapper itemQualityControlSurveyTaskMapper;

    @Resource
    private ItemQualityControlSurveyTaskMaterialsRelationMapper itemQualityControlSurveyTaskMaterialsRelationMapper;

    @Resource
    private ItemQualityControlWhitelistMapper itemQualityControlWhitelistMapper;

    @Resource
    private ItemQualityControlConverter itemQualityControlConverter;

    /**
     * 白名单失效时间
     */
    private final static int WHITELIST_INVALID_TIME_DAYS = 30;

    /**
     * 分批次查询出风险结果数据
     *
     * @return
     */
    public Map<String, ItemQualityControlResultDTO> queryRiskData() {
        // 构造查询条件
        LambdaQueryWrapper<ItemQualityControlResultDO> queryWrapper = Wrappers.lambdaQuery();
        // 未处理的数据
        queryWrapper.eq(ItemQualityControlResultDO::getProcessedFlag, YesOrNoEnum.NO.getCode());

        // 分页查询存放数据的总集合
        Map<String, ItemQualityControlResultDTO> results = new HashMap<>();

        int pageNum = 1;
        // 设置每次查询的数据量，最大为200
        int pageSize = QUERY_ITEM_MAX_COUNT;
        Page<ItemQualityControlResultDO> page = new Page<>(pageNum, pageSize);
        // 查询第一页数据
        Page<ItemQualityControlResultDO> pageResult = itemQualityControlResultMapper.selectPage(page, queryWrapper);

        // 判断是否还有数据，还有数据则页码+1继续执行分页查询
        List<ItemQualityControlResultDO> batchResult = pageResult.getRecords();
        try {
            while (batchResult.size() >= pageSize) {
                putAll(results, batchResult);
                pageNum += 1;
                page.setCurrent(pageNum);
                pageResult = itemQualityControlResultMapper
                        .selectPage(page, queryWrapper);
                batchResult = pageResult.getRecords();
                // 每次循环获取数据后，休眠20ms，避免对数据库造成太大压力
                Thread.sleep(20);
            }
        } catch (InterruptedException e) {
            throw new BaseBizException(ProductExceptionCode.PRODUCT_SQL);
        }

        // 最后一组数据也放入结果集中
        putAll(results, page.getRecords());

        // 转为DTO
        return results;
    }

    private void putAll(Map<String, ItemQualityControlResultDTO> results, List<ItemQualityControlResultDO> batchResult) {
        Map<String, ItemQualityControlResultDTO> batchResultMap = batchResult.stream()
                .collect(Collectors.toMap(ItemQualityControlResultDO::getSurveyId,
                        itemQualityControlResultDO -> itemQualityControlConverter.entityToDTO(itemQualityControlResultDO),
                        (oldKey, newKey) -> oldKey));
        results.putAll(batchResultMap);
    }

    /**
     * 批量写入风险商品列表
     *
     * @param resultList
     */
    public void insertRiskData(Collection<ItemQualityControlResultDTO> resultList) {
        List<ItemQualityControlDataDO> entityList = resultList.stream().map(itemQualityControlResultDTO -> {
            ItemQualityControlDataDO itemQualityControlDataDO = itemQualityControlConverter.dtoToEntity(itemQualityControlResultDTO);
            itemQualityControlDataDO.initCommon();
            return itemQualityControlDataDO;
        }).collect(Collectors.toList());
        itemQualityControlDataMapper.insertBatch(entityList);
    }

    /**
     * 获取风险商品列表中存在的风险数据
     *
     * @param surveyIdSet
     * @return
     */
    public List<ItemQualityControlDataDO> queryExistsInRiskResultData(Set<String> surveyIdSet) {
        LambdaQueryWrapper<ItemQualityControlDataDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(CollectionUtils.isNotEmpty(surveyIdSet), ItemQualityControlDataDO::getSurveyId, surveyIdSet);
        // 已存在的风险数据
        return itemQualityControlDataMapper.selectList(queryWrapper);
    }

    /**
     * 更新风险等级，剔除掉存在的风险数据
     *
     * @param riskDataMap
     * @param existsInRiskResultDataList
     */
    public void updateExistsInRiskResultData(Map<String, ItemQualityControlResultDTO> riskDataMap, List<ItemQualityControlDataDO> existsInRiskResultDataList) {
        for (ItemQualityControlDataDO itemQualityControlDataDO : existsInRiskResultDataList) {
            ItemQualityControlResultDTO itemQualityControlDataDTO = riskDataMap.remove(itemQualityControlDataDO.getSurveyId());
            if (Objects.isNull(itemQualityControlDataDTO)) {
                continue;
            }

            LambdaUpdateWrapper<ItemQualityControlDataDO> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.set(ItemQualityControlDataDO::getRiskLevel, itemQualityControlDataDTO.getRiskLevel());
            updateWrapper.eq(ItemQualityControlDataDO::getSurveyId, itemQualityControlDataDO.getSurveyId());
            itemQualityControlDataMapper.update(null, updateWrapper);
        }
    }

    /**
     * 使数据集市提供的结果数据失效
     *
     * @param surveyIdSet
     */
    public void invalidQualityResult(Set<String> surveyIdSet) {
        LambdaUpdateWrapper<ItemQualityControlResultDO> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.in(ItemQualityControlResultDO::getSurveyId, surveyIdSet);
        updateWrapper.set(ItemQualityControlResultDO::getProcessedFlag, YesOrNoEnum.YES.getCode());
        itemQualityControlResultMapper.update(null, updateWrapper);
    }

    /**
     * 获取调查池中存在的风险数据
     *
     * @param surveyIdSet
     * @return
     */
    public List<ItemQualityControlSurveyDO> queryExistsInSurveyData(Set<String> surveyIdSet) {
        LambdaQueryWrapper<ItemQualityControlSurveyDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(CollectionUtils.isNotEmpty(surveyIdSet), ItemQualityControlSurveyDO::getSurveyId, surveyIdSet);
        queryWrapper.eq(ItemQualityControlSurveyDO::getClaimedStatus, ClaimedStatusEnum.SHELTERS.getCode());
        // 已存在的风险数据
        return itemQualityControlSurveyMapper.selectList(queryWrapper);
    }

    /**
     * 更新风险等级，剔除掉存在的风险数据
     *
     * @param riskDataMap
     * @param existsInSurveyDataList
     */
    public void updateExistsInSurveyData(Map<String, ItemQualityControlResultDTO> riskDataMap, List<ItemQualityControlSurveyDO> existsInSurveyDataList) {
        for (ItemQualityControlSurveyDO itemQualityControlSurveyDO : existsInSurveyDataList) {
            ItemQualityControlResultDTO itemQualityControlDataDTO = riskDataMap.remove(itemQualityControlSurveyDO.getSurveyId());
            if (Objects.isNull(itemQualityControlDataDTO)) {
                continue;
            }

            LambdaUpdateWrapper<ItemQualityControlSurveyDO> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.set(ItemQualityControlSurveyDO::getRiskLevel, itemQualityControlDataDTO.getRiskLevel());
            updateWrapper.eq(ItemQualityControlSurveyDO::getSurveyId, itemQualityControlDataDTO.getSurveyId());
            itemQualityControlSurveyMapper.update(null, updateWrapper);
        }
    }

    /**
     * 获取任务集中存在的风险数据
     *
     * @param surveyIdSet
     * @return
     */
    public List<ItemQualityControlSurveyTaskDO> queryExistsInSurveyTaskData(Set<String> surveyIdSet) {
        LambdaQueryWrapper<ItemQualityControlSurveyTaskDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(CollectionUtils.isNotEmpty(surveyIdSet), ItemQualityControlSurveyTaskDO::getSurveyId, surveyIdSet);
        // 已存在的风险数据
        return itemQualityControlSurveyTaskMapper.selectList(queryWrapper);
    }

    /**
     * 更新风险等级，剔除掉存在的风险数据
     *
     * @param riskDataMap
     * @param pendingTaskList
     */
    public void updateExistsInSurveyTaskData(Map<String, ItemQualityControlResultDTO> riskDataMap, List<ItemQualityControlSurveyTaskDO> pendingTaskList) {
        for (ItemQualityControlSurveyTaskDO itemQualityControlSurveyTaskDO : pendingTaskList) {
            ItemQualityControlResultDTO itemQualityControlDataDTO = riskDataMap.remove(itemQualityControlSurveyTaskDO.getSurveyId());
            if (Objects.isNull(itemQualityControlDataDTO)) {
                continue;
            }

            LambdaUpdateWrapper<ItemQualityControlSurveyTaskDO> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.set(ItemQualityControlSurveyTaskDO::getRiskLevel, itemQualityControlDataDTO.getRiskLevel());
            updateWrapper.eq(ItemQualityControlSurveyTaskDO::getSurveyId, itemQualityControlDataDTO.getSurveyId());
            itemQualityControlSurveyTaskMapper.update(null, updateWrapper);
        }
    }

    /**
     * 剔除白名单中存在的风险数据
     *
     * @param riskDataMap
     * @return
     */
    public Map<String, ItemQualityControlResultDTO> removeExistsInWhitelist(Map<String, ItemQualityControlResultDTO> riskDataMap) {
        Iterator<Map.Entry<String, ItemQualityControlResultDTO>> iterator = riskDataMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, ItemQualityControlResultDTO> entry = iterator.next();
            LambdaQueryWrapper<ItemQualityControlWhitelistDO> queryWrapper = Wrappers.lambdaQuery();
            ItemQualityControlResultDTO itemQualityControlResultDTO = entry.getValue();
            queryWrapper.eq(ItemQualityControlWhitelistDO::getSkuId, itemQualityControlResultDTO.getSkuId());
            queryWrapper.eq(ItemQualityControlWhitelistDO::getSellerId, itemQualityControlResultDTO.getSellerId());
            queryWrapper.eq(ItemQualityControlWhitelistDO::getActiveFlag, YesOrNoEnum.YES.getCode());
            ItemQualityControlWhitelistDO itemQualityControlWhitelistDO = itemQualityControlWhitelistMapper.selectOne(queryWrapper);

            // 在白名单内
            if (Objects.nonNull(itemQualityControlWhitelistDO)) {
                iterator.remove();
            }
        }
        return riskDataMap;
    }

    /**
     * 加入调查池
     *
     * @param request
     */
    public void joinSurveyPool(ItemQualityControlSurveyRequest request) {
        String surveyId = request.getSurveyId();

        // 从风险商品列表中查询出数据
        LambdaQueryWrapper<ItemQualityControlDataDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ItemQualityControlDataDO::getSurveyId, surveyId);
        queryWrapper.eq(ItemQualityControlDataDO::getDelFlag, DelFlagEnum.EFFECTIVE.getCode());
        queryWrapper.in(ItemQualityControlDataDO::getCategoryId, request.getCategoryIdList());

        ItemQualityControlDataDO itemQualityControlDataDO = itemQualityControlDataMapper.selectOne(queryWrapper);
        if (Objects.isNull(itemQualityControlDataDO)) {
            throw new BaseBizException(CommonErrorCodeEnum.SQL_ERROR);
        }

        // 转换
        ItemQualityControlSurveyDO itemQualityControlSurveyDO = itemQualityControlConverter.convertQualityData(itemQualityControlDataDO);
        itemQualityControlSurveyDO.setClaimedStatus(ClaimedStatusEnum.SHELTERS.getCode());
        itemQualityControlDataDO.initCommon();
        // 插入
        int count = itemQualityControlSurveyMapper.insert(itemQualityControlSurveyDO);
        if (count <= 0) {
            throw new BaseBizException(CommonErrorCodeEnum.SQL_ERROR);
        }
    }

    /**
     * 使风险商品数据失效
     *
     * @param dto
     */
    public ItemQualityControlDataDO invalidQualityData(ItemQualityControlDataDTO dto) {
        String surveyId = dto.getSurveyId();
        LambdaQueryWrapper<ItemQualityControlDataDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ItemQualityControlDataDO::getSurveyId, surveyId);
        ItemQualityControlDataDO itemQualityControlDataDO = itemQualityControlDataMapper.selectOne(queryWrapper);
        if (Objects.isNull(itemQualityControlDataDO)) {
            throw new BaseBizException(CommonErrorCodeEnum.SQL_ERROR);
        }

        // 用户是否与权限操作该风险数据
        if (!dto.getCategoryIdList().contains(itemQualityControlDataDO.getCategoryId())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        // 失效处理
        LambdaUpdateWrapper<ItemQualityControlDataDO> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(ItemQualityControlDataDO::getSurveyId, surveyId);
        updateWrapper.set(ItemQualityControlDataDO::getDelFlag, DelFlagEnum.DISABLED.getCode());
        int count = itemQualityControlDataMapper.update(null, updateWrapper);
        if (count <= 0) {
            throw new BaseBizException(CommonErrorCodeEnum.SQL_ERROR);
        }

        return itemQualityControlDataDO;
    }

    /**
     * 流入白名单
     *
     * @param itemQualityControlDataDO
     */
    public void joinWhitelist(ItemQualityControlDataDO itemQualityControlDataDO) {
        // 转换
        ItemQualityControlWhitelistDO itemQualityControlWhitelistDO = itemQualityControlConverter.convertQualityWhitelist(itemQualityControlDataDO);
        // 设置过期时间
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime invalidTime = now.plusDays(WHITELIST_INVALID_TIME_DAYS);
        Date deadline = LocalDateTimeUtil.localDateTimeToDate(invalidTime);
        itemQualityControlWhitelistDO.setDeadline(deadline);
        itemQualityControlDataDO.initCommon();

        int count = itemQualityControlWhitelistMapper.insert(itemQualityControlWhitelistDO);
        if (count <= 0) {
            throw new BaseBizException(CommonErrorCodeEnum.SQL_ERROR);
        }
    }

    /**
     * 获取调查池中的数据
     *
     * @param request
     * @return
     */
    public ItemQualityControlSurveyDO querySurveyPool(ItemQualityControlSurveyTaskRequest request) {
        LambdaQueryWrapper<ItemQualityControlSurveyDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Objects.nonNull(request.getSurveyId()), ItemQualityControlSurveyDO::getSurveyId, request.getSurveyId());

        ItemQualityControlSurveyDO itemQualityControlSurveyDO = itemQualityControlSurveyMapper.selectOne(queryWrapper);
        if (Objects.isNull(itemQualityControlSurveyDO)) {
            throw new BaseBizException(CommonErrorCodeEnum.SQL_ERROR);
        }

        return itemQualityControlSurveyDO;
    }

    /**
     * 认领调查池中的风险数据
     *
     * @param itemQualityControlSurveyDO
     */
    public void claimedSurveyTask(ItemQualityControlSurveyDO itemQualityControlSurveyDO, ItemQualityControlSurveyTaskRequest request) {
        // 调查池中的数据设置为已认领
        LambdaUpdateWrapper<ItemQualityControlSurveyDO> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(Objects.nonNull(itemQualityControlSurveyDO.getSurveyId()), ItemQualityControlSurveyDO::getSurveyId, itemQualityControlSurveyDO.getSurveyId());
        updateWrapper.set(ItemQualityControlSurveyDO::getClaimedStatus, ClaimedStatusEnum.CLAIMED.getCode());
        int count = itemQualityControlSurveyMapper.update(null, updateWrapper);
        if (count <= 0) {
            throw new BaseBizException(CommonErrorCodeEnum.SQL_ERROR);
        }

        // 转换
        ItemQualityControlSurveyTaskDO itemQualityControlSurveyTaskDO = itemQualityControlConverter.convertQualityTask(itemQualityControlSurveyDO);
        // 填充调查任务数据对象
        fillItemQualityControlSurveyTaskDO(itemQualityControlSurveyTaskDO, request);
        // 写入调查任务表
        count = itemQualityControlSurveyTaskMapper.insert(itemQualityControlSurveyTaskDO);
        if (count <= 0) {
            throw new BaseBizException(CommonErrorCodeEnum.SQL_ERROR);
        }
    }

    /**
     * 填充调查任务数据对象
     *
     * @param itemQualityControlSurveyTaskDO
     */
    private void fillItemQualityControlSurveyTaskDO(ItemQualityControlSurveyTaskDO itemQualityControlSurveyTaskDO, ItemQualityControlSurveyTaskRequest request) {
        // 获取用户ID
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if (Objects.isNull(loginUser)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
        Long userId = loginUser.getUserId();

        itemQualityControlSurveyTaskDO.setUserId(userId.intValue());
        itemQualityControlSurveyTaskDO.setSurveyStatus(QualitySurveyStatusEnum.PENDING.getCode());
        itemQualityControlSurveyTaskDO.setAuditStatus(QualityAuditStatusEnum.PENDING.getCode());
        itemQualityControlSurveyTaskDO.setQualityUploadStatus(QualityUploadStatusEnum.NOT_UPLOADED.getCode());
        itemQualityControlSurveyTaskDO.setDeadlineStatus(YesOrNoEnum.YES.getCode());

        // 设置商家上传资质证明材料的截止期限
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime deadline = now.plusDays(7);
        Date date = LocalDateTimeUtil.localDateTimeToDate(deadline);
        itemQualityControlSurveyTaskDO.setDeadline(date);
        itemQualityControlSurveyTaskDO.initCommon();
    }

    /**
     * 获取超过期限的生效状态的白名单记录
     *
     * @return
     */
    public List<ItemQualityControlWhitelistDO> queryWhitelist() {
        LambdaQueryWrapper<ItemQualityControlWhitelistDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ItemQualityControlWhitelistDO::getActiveFlag, YesOrNoEnum.YES.getCode());
        queryWrapper.lt(ItemQualityControlWhitelistDO::getDeadline, new Date());

        List<ItemQualityControlWhitelistDO> itemQualityControlWhitelistDOS = itemQualityControlWhitelistMapper.selectList(queryWrapper);
        return CollectionUtils.isEmpty(itemQualityControlWhitelistDOS) ? Collections.emptyList() : itemQualityControlWhitelistDOS;
    }

    /**
     * 设置为失效状态
     *
     * @param itemQualityControlWhitelistDOList
     */
    public void invalidWhitelist(List<ItemQualityControlWhitelistDO> itemQualityControlWhitelistDOList) {
        LambdaUpdateWrapper<ItemQualityControlWhitelistDO> updateWrapper = Wrappers.lambdaUpdate();
        long sum = itemQualityControlWhitelistDOList.stream().map(itemQualityControlWhitelistDO -> {
            updateWrapper.eq(ItemQualityControlWhitelistDO::getSkuId, itemQualityControlWhitelistDO.getSkuId());
            updateWrapper.eq(ItemQualityControlWhitelistDO::getSellerId, itemQualityControlWhitelistDO.getSellerId());
            updateWrapper.set(ItemQualityControlWhitelistDO::getActiveFlag, YesOrNoEnum.NO.getCode());
            return itemQualityControlWhitelistMapper.update(null, updateWrapper);
        }).mapToInt(count -> count).sum();

        if (sum < itemQualityControlWhitelistDOList.size()) {
            throw new ProductBizException(CommonErrorCodeEnum.SQL_ERROR);
        }
    }

    /**
     * 获取风险商品列表
     *
     * @param request
     * @return
     */
    public List<ItemQualityControlDataDTO> riskSkuList(ItemQualityControlDataRequest request) {
        LambdaQueryWrapper<ItemQualityControlDataDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Objects.nonNull(request.getSkuId()), ItemQualityControlDataDO::getSkuId, request.getSkuId());
        queryWrapper.eq(Objects.nonNull(request.getSellerId()), ItemQualityControlDataDO::getSellerId, request.getSellerId());
        queryWrapper.eq(Objects.nonNull(request.getSurveyId()), ItemQualityControlDataDO::getSurveyId, request.getSurveyId());
        queryWrapper.eq(Objects.nonNull(request.getRiskLevel()), ItemQualityControlDataDO::getRiskLevel, request.getRiskLevel());
        queryWrapper.in(Objects.nonNull(request.getCategoryIdList()), ItemQualityControlDataDO::getCategoryId, request.getCategoryIdList());

        Page<ItemQualityControlDataDO> page = new Page<>(request.getPageNo(), request.getPageSize());
        Page<ItemQualityControlDataDO> pageResult = itemQualityControlDataMapper.selectPage(page, queryWrapper);

        return itemQualityControlConverter.riskDataListEntityToDTO(pageResult.getRecords());
    }

    /**
     * 获取调查池列表
     *
     * @param request
     * @return
     */
    public List<ItemQualityControlSurveyDTO> surveyPoolList(ItemQualityControlSurveyRequest request) {
        LambdaQueryWrapper<ItemQualityControlSurveyDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Objects.nonNull(request.getSkuId()), ItemQualityControlSurveyDO::getSkuId, request.getSkuId());
        queryWrapper.eq(Objects.nonNull(request.getSellerId()), ItemQualityControlSurveyDO::getSellerId, request.getSellerId());
        queryWrapper.eq(Objects.nonNull(request.getSurveyId()), ItemQualityControlSurveyDO::getSurveyId, request.getSurveyId());
        queryWrapper.eq(Objects.nonNull(request.getRiskLevel()), ItemQualityControlSurveyDO::getRiskLevel, request.getRiskLevel());
        queryWrapper.eq(Objects.nonNull(request.getClaimedStatus()), ItemQualityControlSurveyDO::getClaimedStatus, request.getClaimedStatus());
        queryWrapper.in(Objects.nonNull(request.getCategoryIdList()), ItemQualityControlSurveyDO::getCategoryId, request.getCategoryIdList());

        Page<ItemQualityControlSurveyDO> page = new Page<>(request.getPageNo(), request.getPageSize());
        Page<ItemQualityControlSurveyDO> pageResult = itemQualityControlSurveyMapper.selectPage(page, queryWrapper);

        return itemQualityControlConverter.surveyPoolListEntityToDTO(pageResult.getRecords());
    }

    /**
     * 获取调查任务列表
     *
     * @param request
     * @return
     */
    public List<ItemQualityControlSurveyTaskDTO> surveyTaskList(ItemQualityControlSurveyTaskRequest request) {
        // 获取用户ID
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if (Objects.isNull(loginUser)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
        Long userId = loginUser.getUserId();

        LambdaQueryWrapper<ItemQualityControlSurveyTaskDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Objects.nonNull(request.getSkuId()), ItemQualityControlSurveyTaskDO::getSkuId, request.getSkuId());
        queryWrapper.eq(Objects.nonNull(request.getSellerId()), ItemQualityControlSurveyTaskDO::getSellerId, request.getSellerId());
        queryWrapper.eq(Objects.nonNull(request.getSurveyId()), ItemQualityControlSurveyTaskDO::getSurveyId, request.getSurveyId());
        queryWrapper.eq(Objects.nonNull(request.getRiskLevel()), ItemQualityControlSurveyTaskDO::getRiskLevel, request.getRiskLevel());
        queryWrapper.eq(Objects.nonNull(request.getSurveyStatus()), ItemQualityControlSurveyTaskDO::getSurveyStatus, request.getSurveyStatus());
        queryWrapper.eq(Objects.nonNull(request.getAuditStatus()), ItemQualityControlSurveyTaskDO::getAuditStatus, request.getAuditStatus());
        queryWrapper.eq(Objects.nonNull(request.getProblemType()), ItemQualityControlSurveyTaskDO::getProblemType, request.getProblemType());
        queryWrapper.eq(Objects.nonNull(request.getQualityUploadStatus()), ItemQualityControlSurveyTaskDO::getQualityUploadStatus, request.getQualityUploadStatus());
        queryWrapper.eq(Objects.nonNull(request.getDeadlineStatus()), ItemQualityControlSurveyTaskDO::getDeadlineStatus, request.getDeadlineStatus());
        queryWrapper.eq(ItemQualityControlSurveyTaskDO::getUserId, userId);

        Page<ItemQualityControlSurveyTaskDO> page = new Page<>(request.getPageNo(), request.getPageSize());
        Page<ItemQualityControlSurveyTaskDO> pageResult = itemQualityControlSurveyTaskMapper.selectPage(page, queryWrapper);

        return itemQualityControlConverter.surveyTaskListEntityToDTO(pageResult.getRecords());
    }
}
